﻿using System;
using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_mapping_a_nested_nested_relation
    {
        public class GuiPerson
        {
            public String Name { get; set; }
            public GuiAddress Address { get; set; }
        }

        public class GuiAddress
        {
            public String Country { get; set; }
        }

        public class DomainPerson
        {
            public String Name { get; set; }
            public DomainAddress Address { get; set; }
        }

        public class DomainAddress
        {
            public DomainCountry Country { get; set; }
        }

        public class DomainCountry
        {
            public String Name { get; set; }
        }

        [Fact]
        public void Should_succeed_when_flattening_inner_nesting()
        {
            var countryMapping = new Mapping<DomainCountry, GuiAddress>();
            countryMapping.Relate(domain=>domain.Name,gui=>gui.Country);
            
            var addressMap = new Mapping<DomainAddress, GuiAddress>();
            addressMap.Flatten(domain=>domain.Country,countryMapping);
            
            var personMapping = new Mapping<DomainPerson, GuiPerson>();
            personMapping.AddConverter(addressMap);
            personMapping.AutoRelateEqualNames();

            var domainPerson = new DomainPerson
                                   {Address = new DomainAddress {Country = new DomainCountry {Name = "NestedCountry"}}};
            var guiPerson = personMapping.Map(
                domainPerson,
                new GuiPerson {Address = new GuiAddress()});
            
            Assert.Equal(domainPerson.Address.Country.Name,guiPerson.Address.Country);
        }
    }
}
